/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/5/20
 * \* Time: 21:14
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 删除排序链表中的重复元素II {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }

        //虚拟头结点指向头结点
        ListNode dummyNode=new ListNode(-101);
        dummyNode.next=head;
        //pre指针指向cur1指针的前一个节点
        ListNode pre = dummyNode;
        //cur1指针指向当前节点
        ListNode cur1 = head;
        //cur2指针用来遍历
        ListNode cur2 = head;
        //记录数字出现的次数
        int count = 0;
        while (cur2!=null){
            while (cur2!=null&&cur2.val==cur1.val){
                cur2=cur2.next;
                count++;
            }

            if(count==1){
                pre=pre.next;
                cur1=cur1.next;
            }else{
                pre.next=cur2;
                cur1=cur2;
            }
            count=0;
        }

        return dummyNode.next;

    }
}